﻿Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports Microsoft.Win32

<Serializable()> _
Public Class UidSettings
    '設定を保存するフィールド

    Public Shared isChanged As Boolean = False

    '既読状態の最大UID
    Private _maxUid As String
    'エラーUID
    Private _errUids As New List(Of String)
    Public Property MaxUid As String
        Get
            Return _maxUid
        End Get
        Set(ByVal Value As String)
            'If _maxUid < Value OrElse _maxUid.Length < Value.Length Then
            '    _maxUid = Value
            '    isChanged = True
            'End If
            _maxUid = Value
            isChanged = True
        End Set
    End Property
    Public Property ErrUids As List(Of String)
        Get
            Return _errUids
        End Get
        Set(ByVal Value As List(Of String))
            _errUids = Value
            isChanged = True
        End Set
    End Property

    'コンストラクタ
    Public Sub New()
        'Me._maxUid = "000"
        'Me._errUid.Add("1001")
        'Me._errUid.Add("1002")
        'Me._errUid.Add("1003")
    End Sub

    'Settingsクラスのただ一つのインスタンス
    <NonSerialized()> _
    Private Shared _instance As UidSettings
    <System.Xml.Serialization.XmlIgnore()> _
    Public Shared Property Instance() As UidSettings
        Get
            If _instance Is Nothing Then
                _instance = New UidSettings
            End If
            Return _instance
        End Get
        Set(ByVal Value As UidSettings)
            _instance = Value
        End Set
    End Property

    ''' <summary>
    ''' 設定をXMLファイルから読み込み復元する
    ''' </summary> 
    ''' <param name="strSettingPath">配置ファイルのパス</param>
    ''' <returns>読み込み結果</returns>
    ''' <exception cref="Exception">読み込み失敗</exception>
    Public Shared Function LoadFromXmlFile(ByVal strSettingPath As String) As Boolean
        Dim p As String = strSettingPath
        Try

            Dim fs As New FileStream(p, FileMode.Open, FileAccess.Read)
            Dim xs As New System.Xml.Serialization.XmlSerializer(GetType(UidSettings))
            '読み込んで逆シリアル化する
            Dim obj As Object = xs.Deserialize(fs)
            fs.Close()

            Instance = CType(obj, UidSettings)

            Return True
        Catch ex As Exception
            SystemError.SystemLog(ex)
            Return False
        End Try
    End Function

    ''' <summary>
    ''' 現在の設定をXMLファイルに保存する
    ''' </summary>
    ''' <param name="strSettingPath">配置ファイルのパス</param>
    ''' <returns>保存結果</returns>
    ''' <exception cref="Exception">保存失敗</exception>
    Public Shared Function SaveToXmlFile(ByVal strSettingPath As String) As Boolean
        Dim p As String = strSettingPath

        Try
            Dim fs As New FileStream( _
                p, FileMode.Create, FileAccess.Write)
            Dim xs As New System.Xml.Serialization.XmlSerializer( _
                GetType(UidSettings))
            'シリアル化して書き込む
            xs.Serialize(fs, Instance)
            fs.Close()

            Return True
        Catch ex As Exception
            SystemError.SystemLog(ex)
            Return False
        End Try
    End Function

End Class